﻿@namespace AntSK.Pages.Setting.AIModel
@using AntSK.Domain.Repositories
@using AntSK.Models
@using AntSK.Domain.Domain.Model.Enum
@using AntSK.LLamaFactory.Model
@using AntSK.LLamaFactory
@using BlazorComponents.Terminal
@page "/modelmanager/model/add"
@page "/modelmanager/model/add/{ModelId}"
@using AntSK.Services.Auth
@inherits AuthComponentBase
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize(Roles = "AntSKAdmin")]

<PageContainer Title="新增模型">
    <ChildContent>
        <Card>
            <Form Model="@_aiModel"
                  Style="margin-top: 8px;"
                  OnFinish="HandleSubmit"
                  Autocomplete="off">
                <FormItem Label="模型描述" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <Input Placeholder="请输入模型描述" @bind-Value="@context.ModelDescription" />
                </FormItem>

                <FormItem Label="模型类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <RadioGroup @bind-Value="context.AIModelType" ButtonStyle="RadioButtonStyle.Solid" OnChange="AIModelTypeChange" TValue="AIModelType">
                        <Radio RadioButton Value="@(AIModelType.Chat)">会话模型</Radio>
                        <Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
                        <Radio RadioButton Value="@(AIModelType.Rerank)">Rerank重排模型</Radio>
                    </RadioGroup>
                </FormItem>

                <FormItem Label="AI类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <RadioGroup @bind-Value="context.AIType" ButtonStyle="RadioButtonStyle.Solid" OnChange="AITypeChange" TValue="AIType">
                        @if (context.AIModelType == AIModelType.Chat)
                        {
                            <Radio RadioButton Value="@(AIType.OpenAI)">OpenAI</Radio>
                            <Radio RadioButton Value="@(AIType.AzureOpenAI)">AzureOpenAI</Radio>
                            <Radio RadioButton Value="@(AIType.SparkDesk)">星火大模型</Radio>
                            <Radio RadioButton Value="@(AIType.DashScope)">灵积大模型</Radio>
                            <Radio RadioButton Value="@(AIType.Ollama)">Ollama本地模型</Radio>
                            <Radio RadioButton Value="@(AIType.LLamaFactory)">LLamaFactory本地模型</Radio>
                            <Radio RadioButton Value="@(AIType.Mock)">模拟输出</Radio>
                        }
                        else if (context.AIModelType == AIModelType.Embedding)
                        {
                            <Radio RadioButton Value="@(AIType.BgeEmbedding)">BgeEmbedding</Radio>
                            <Radio RadioButton Value="@(AIType.OpenAI)">OpenAI</Radio>
                            <Radio RadioButton Value="@(AIType.AzureOpenAI)">AzureOpenAI</Radio>
                            <Radio RadioButton Value="@(AIType.OllamaEmbedding)">Ollama本地模型</Radio>
                        }
                        else if (context.AIModelType == AIModelType.Rerank)
                        {
                            <Radio RadioButton Value="@(AIType.BgeRerank)">BgeRerank</Radio>
                        }
                    </RadioGroup>
                </FormItem>

                @if (context.AIModelType == AIModelType.Embedding)
                {
                    <FormItem Label="注意事项" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <span style="color:red"><b>请不要使用不同维度的向量模型，否则会导致无法向量存储</b></span>
                    </FormItem>
                }
                @if (context.AIType == AIType.AzureOpenAI)
                {
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址" @bind-Value="@context.EndPoint" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="部署名(模型名)" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入部署名" @bind-Value="@context.ModelName" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥" AutoComplete="false" />
                    </FormItem>
                }
                @if (context.AIType == AIType.OpenAI)
                {
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址 示例格式 http://ip:port/" @bind-Value="@context.EndPoint" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入模型名称" @bind-Value="@context.ModelName" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥" AutoComplete="false" />
                    </FormItem>
                }
                @if (context.AIType == AIType.SparkDesk)
                {
                    <FormItem Label="模型版本" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <SimpleSelect DefaultValue="Lite" @bind-Value="@context.ModelName">
                            <SelectOptions>
                                <SimpleSelectOption Value="V3_5" Label="Max"></SimpleSelectOption>
                                <SimpleSelectOption Value="V3" Label="Pro"></SimpleSelectOption>
                                <SimpleSelectOption Value="V2" Label="V2"></SimpleSelectOption>
                                <SimpleSelectOption Value="V1_5" Label="Lite"></SimpleSelectOption>
                            </SelectOptions>
                        </SimpleSelect>
                    </FormItem>
                    <FormItem Label="请输入模型APPID|APISecret|APIKey" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword Placeholder="APPID|APISecret|APIKey" @bind-Value="@context.ModelKey" AutoComplete="false" />
                    </FormItem>

                }
                @if (context.AIType == AIType.DashScope)
                {
                    <FormItem Label="API KEY" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入API KEY" @bind-Value="@context.ModelKey" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入模型名称" @bind-Value="@context.ModelName" AutoComplete="false" />
                    </FormItem>
                }
                @if (context.AIType == AIType.LLamaFactory)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Select TItem="LLamaModel"
                                    TItemValue="string"
                                    DataSource="@modelList"
                                    @bind-Value="@context.ModelName"
                                    LabelName="@nameof(LLamaModel.Name)"
                                    ValueName="@nameof(LLamaModel.ModelScope)"
                                    Placeholder="选择模型"
                                    DefaultActiveFirstOption="false"
                                    EnableSearch
                                    OnSearch="OnSearch">
                            </Select>
                        </InputGroup>
                    </FormItem>
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="http://localhost:8080/" @bind-Value="@context.EndPoint" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="基础环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">开始安装</Button>
                    </FormItem>

                    <FormItem Label="缺失依赖包安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstallShow">pip install</Button>
                    </FormItem>
                    <FormItem Label="llama factory服务" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            @if (!llamaFactoryIsStart)
                            {
                                <Button Type="primary" OnClick="StartLFService" Disabled="@(llamaFactoryIsStart)">启动</Button>
                            }
                            else
                            {
                                <Button OnClick="StopLFService" Disabled="@(!llamaFactoryIsStart)">停止</Button>
                            }
                        </InputGroup>
                    </FormItem>
                }
                @if (context.AIType == AIType.Ollama)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Select TItem="string"
                                    TItemValue="string"
                                    DataSource="@ollamaModelList"
                                    @bind-Value="@context.ModelName"
                                    ValueProperty="c=>c"
                                    LabelProperty="c=>c"
                                    Placeholder="选择模型"
                                    DefaultActiveFirstOption="false"
                                    EnableSearch
                                    OnSearch="OnOllamaSearch">
                            </Select>
                        </InputGroup>
                        <a href="https://ollama.com/library" target="_blank">获取更多模型</a>
                        <br/>
                        <span>可以在OllamaModelList.txt中进行修改</span>
                    </FormItem>
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="http://localhost:11434/" @bind-Value="@context.EndPoint" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <a href="https://ollama.com/download" target="_blank">下载Ollama</a>
                    </FormItem>
                    <FormItem Label="Ollama服务" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Button Type="primary" OnClick="StartOllamaService">下载模型</Button>
                        </InputGroup>
                    </FormItem>
                }
                @if (context.AIType == AIType.OllamaEmbedding)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Select TItem="string"
                                    TItemValue="string"
                                    DataSource="@ollamaEmbeddingModelList"
                                    @bind-Value="@context.ModelName"
                                    ValueProperty="c=>c"
                                    LabelProperty="c=>c"
                                    Placeholder="选择模型"
                                    DefaultActiveFirstOption="false"
                                    EnableSearch
                                    OnSearch="OnOllamaEmbeddingSearch">
                            </Select>
                        </InputGroup>
                        <a href="https://ollama.com/library" target="_blank">获取更多模型</a>
                        <br />
                        <span>可以在OllamaEmbeddingModelList.txt中进行修改</span>
                    </FormItem>
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="http://localhost:11434/" @bind-Value="@context.EndPoint" AutoComplete="false" />
                    </FormItem>
                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <a href="https://ollama.com/download" target="_blank">下载Ollama</a>
                    </FormItem>
                    <FormItem Label="Ollama服务" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Button Type="primary" OnClick="StartOllamaService">下载模型</Button>
                        </InputGroup>
                    </FormItem>
                }
                @if (context.AIType == AIType.BgeEmbedding)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Select DataSource="@bgeEmbeddingList"
                                @bind-Value="@context.ModelName"
                                ValueProperty="c=>c"
                                LabelProperty="c=>c">
                        </Select>  
                    </FormItem>
                    <FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
                            <Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" AutoComplete="false" />
                        </Popover>
                    </FormItem>

                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">环境安装</Button>
                    </FormItem>
                    <FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Spin Tip="请等待..." Spinning="@(BgeEmbeddingIsStart)">
                            <Button Type="primary" Disabled="@(BgeEmbeddingIsStart)" OnClick="BgeEmbedding">@BgeEmbeddingBtnText</Button>
                        </Spin>
                    </FormItem>
                }
                @if (context.AIType == AIType.BgeRerank)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Select DataSource="@bgeRerankList"
                                @bind-Value="@context.ModelName"
                                ValueProperty="c=>c"
                                LabelProperty="c=>c">
                        </Select>
                    </FormItem>
                    <FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
                            <Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" AutoComplete="false" />
                        </Popover>
                    </FormItem>
                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">环境安装</Button>
                    </FormItem>
                    <FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Spin Tip="请等待..." Spinning="@(BgeRerankIsStart)">
                            <Button Type="primary" Disabled="@(BgeRerankIsStart)" OnClick="BgeRerank">@BgeRerankBtnText</Button>
                        </Spin>
                    </FormItem>
                }
                @if (context.AIType == AIType.Mock)
                {
                }
                <FormItem Label=" " Style="margin-top:32px" WrapperCol="LayoutModel._submitFormLayout.WrapperCol">
                    <Button Type="primary" OnClick="HandleSubmit">
                        保存
                    </Button>
                    <Button OnClick="Back">
                        返回
                    </Button>
                </FormItem>
            </Form>
        </Card>
    </ChildContent>
</PageContainer>

<Modal @ref="_modal" Visible="_downloadModalVisible" Footer="null" Closable Title="模型下载" OnCancel="OnCancelDownload" DestroyOnClose>
    <Flex Gap="10" Vertical>
        <InputGroup>
            <Input Disabled="_downloadStarted" Placeholder="请输入下载地址" @bind-Value="_downloadUrl" Style="width:80%"></Input>
            @if (!_downloadStarted)
            {
                <Button OnClick="StartDownload" Style="margin-left:10px;" Type="@ButtonType.Primary">开始</Button>
            }
            else
            {
                <Button OnClick="StopDownload">停止</Button>
            }
        </InputGroup>
        <AntDesign.Progress Percent="_downloadProgress"></AntDesign.Progress>

    </Flex>
</Modal>

<Modal @ref="_modal" Visible="_pipModalVisible" Footer="null" Closable Title="pip install" OnCancel="OnCancelPipInstall" DestroyOnClose>
    <Flex Gap="10" Vertical>
        <InputGroup>
            <Input Placeholder="请输入包名（例如 pip install torch 则只需要输入 torch）" @bind-Value="_pipName" Style="border-radius: 8px"></Input>
        </InputGroup>
        <InputGroup>
            <Button OnClick="PipInstallName" Type="@ButtonType.Primary">pip install</Button>
        </InputGroup>
    </Flex>
</Modal>

<Modal Visible="_logModalVisible" Footer="null" Closable Title="日志输出" OnCancel="OnCancelLog" DestroyOnClose Width="1000">
    <BlazorTerminal @ref="blazorTerminal" Style="height:600px;overflow-y: auto;"></BlazorTerminal>
</Modal>


@code
{
    RenderFragment _content =
    @<div>
        <p>在windows上使用 python311.dll文件，在linux上使用 libpython3.10.so</p>
    </div>;
}